<template>
  <div class="apk-grid-page">
    <div class="header">
      <h1>移动端应用作品</h1>
      <p>我的作品请扫码下载体验</p>
    </div>

    <div class="apk-grid">
      <div
          v-for="apk in apkList"
          :key="apk.id"
          class="apk-card"
          @mouseenter="showQRCode(apk.id)"
          @mouseleave="hideQRCode()"
          @click="apkCardClick(apk.id)">
        <div class="apk-icon">
          <img 
            v-if="apk.icon && !apk.iconLoadError" 
            :src="apk.icon" 
            :alt="apk.name"
            @error="handleIconError(apk.id)">
          <div 
            v-else 
            class="fallback-icon"
            :style="{ backgroundColor: getRandomColor(apk.id) }">
            <span class="fallback-text">{{ apk.name }}</span>
          </div>
        </div>
        <div class="apk-info">
          <h3>{{ apk.name }}</h3>
          <p class="version-size">
            <span class="version">版本: {{ apk.version }}</span>
            <span class="size">大小: {{ apk.size }}</span>
          </p>
          <p class="description">{{ apk.description }}</p>
        </div>

        <!-- 下载按钮 -->
        <button class="download-btn">查看详情</button>

        <div v-if="activeQRCode === apk.id"
             class="qr-overlay"
             @click="hideQRCode()">
          <div class="qr-container">
            <div :ref="(el) => qrCodeRefs[apk.id] = el" class="qr-canvas"></div>
            <p>扫描二维码下载</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import {ref, onMounted, nextTick} from 'vue'
import QRCode from 'qrcode'
import {apkListData} from '../data/apkList.js'

const activeQRCode = ref(null)
const qrCodeRefs = ref({})
const apkList = ref([])

// 初始化APK列表数据
onMounted(() => {
  // 这里可以从接口获取数据，目前使用示例数据
  apkList.value = apkListData.map(apk => ({
    ...apk,
    iconLoadError: false
  }))
})

// 处理图标加载错误
const handleIconError = (id) => {
  const apk = apkList.value.find(item => item.id === id)
  if (apk) {
    apk.iconLoadError = true
  }
}

// 获取随机颜色
const getRandomColor = (id) => {
  // 使用ID确保颜色一致性
  const colors = [
    '#FF6B6B', '#4ECDC4', '#45B7D1', '#FFBE0B', '#FB5607',
    '#8338EC', '#3A86FF', '#38B000', '#9EF01A', '#FF006E'
  ]
  return colors[id % colors.length]
}

const showQRCode = async (id) => {
  activeQRCode.value = id

  await nextTick()

  const apk = apkList.value.find(apk => apk.id === id)
  const qrCodeElement = qrCodeRefs.value[id]

  if (qrCodeElement) {
    // 清空之前的二维码
    qrCodeElement.innerHTML = ''

    // 生成新的二维码
    try {
      // 使用 qrcode 库生成二维码并渲染到 canvas
      const canvas = document.createElement('canvas')
      await QRCode.toCanvas(canvas, apk.downloadUrl || `${apk.name}应用下载`, {
        width: 200,
        margin: 2,
        color: {
          dark: '#000000',
          light: '#ffffff'
        }
      })

      // 将 canvas 添加到 qrCodeElement 中
      qrCodeElement.appendChild(canvas)
    } catch (err) {
      console.error('生成二维码失败:', err)
    }
  }
}

const apkCardClick = (id) => {
  showQRCode(id)
}

const hideQRCode = () => {
  activeQRCode.value = null
}
</script>

<style scoped>
.apk-grid-page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4edf9 100%);
  min-height: 100vh;
}

.header {
  text-align: center;
  margin-bottom: 40px;
  padding: 40px 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
  color: white;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.header h1 {
  font-size: 2.5rem;
  color: white;
  margin-bottom: 10px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.header p {
  font-size: 1.2rem;
  color: rgba(255, 255, 255, 0.9);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.hint {
  text-align: center;
  margin-bottom: 30px;
  color: #888;
  font-style: italic;
}

.apk-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 30px;
}

@media (min-width: 992px) {
  .apk-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

.apk-card {
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(102, 126, 234, 0.15);
  padding: 20px;
  position: relative;
  transition: all 0.3s ease;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  min-height: 300px;
  border: 1px solid rgba(102, 126, 234, 0.1);
  overflow: hidden;
}

.apk-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 30px rgba(102, 126, 234, 0.25);
  border-color: rgba(102, 126, 234, 0.3);
  border-radius: 12px;
}

.apk-icon {
  text-align: center;
  margin-bottom: 15px;
}

.apk-icon img {
  width: 80px;
  height: 80px;
  border-radius: 16px;
}

.fallback-icon {
  width: 80px;
  height: 80px;
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  margin: 0 auto;
  padding: 5px;
  text-align: center;
}

.fallback-text {
  font-size: 14px;
  font-weight: bold;
  word-break: break-all;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.apk-info h3 {
  font-size: 1.3rem;
  margin-bottom: 4px;
  color: #667eea;
  font-weight: 600;
}

.version-size {
  display: flex;
  justify-content: center;
  gap: 15px;
  font-size: 0.9rem;
  color: #666;
  margin: 5px 0;
}

.description {
  font-size: 0.95rem;
  color: #555;
  margin-top: 8px;
  line-height: 1.5;
}

.download-btn {
  margin-top: auto;
  padding: 8px 16px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
}

.download-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

.qr-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(102, 126, 234, 0.8);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  cursor: default;
}

.qr-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  width: 100%;
  background: white;
  padding: 30px;
  border-radius: 12px;
  text-align: center;
  box-shadow: 0 10px 30px rgba(102, 126, 234, 0.4);
  border: 1px solid #667eea;
}

.qr-canvas {
  width: 100%;
  height: 200px;
  margin-bottom: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.qr-container p {
  font-size: 0.9rem;
  color: #667eea;
  margin: 0;
  font-weight: 600;
}

@media (max-width: 768px) {
  .apk-grid {
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 20px;
  }

  .header h1 {
    font-size: 2rem;
  }

  .header p {
    font-size: 1rem;
  }
}

@media (max-width: 576px) {
  .apk-grid {
    grid-template-columns: 1fr;
  }
}

/* 添加媒体查询以适配小屏幕 */
@media (max-width: 768px) {
  .header {
    padding: 30px 15px;
  }
}
</style>